package com.librariy.json;

/**
 * 列表框通用选项对象
 * 
 * @author luchyu
 */
public class OptionItem extends JsonArray implements CharSequence {
    private static final long serialVersionUID = 1L;
    public OptionItem(String mLable, Object...mValues) {
        super.add(mLable);
        if(mValues==null){
           return;
        }
        for(Object item:mValues){
            super.add(item);
        }
    }
    @Override
    public String toString() {
        return optString(0,"");
    }
    @Override
    public int length() {
        return toString().length();
    }
    @Override
    public char charAt(int index) {
        return toString().charAt(index);
    }

    @Override
    public CharSequence subSequence(int start, int end) {
        return toString().subSequence(start, end);
    }
    @Override 
    public synchronized int hashCode() {        
        return 0;
    }
    @Override
    public boolean equals(Object o) {
        try {
            if(o==this) return true;
            if(o==null||this==null) return false;
            if(!(o instanceof OptionItem)){
                return false;
            }
            OptionItem o1=(OptionItem)o;
            if(super.size()!=o1.size()){
                return false;
            }
            for(int i=0;i<super.size();i++){
                String v1=super.optString(i,null);
                String v2=o1.optString(i,null);
                if(v1==v2) continue;
                if(v1!=null&&v1.equals(v2)) continue;
                return false;
            }
            return true;
        } catch (Exception e) {
            return super.equals(o);
        }
    }
    public static OptionItem create(String mLable, String json) {
        return new OptionItem(mLable,new JsonObject(json));
    }
    public static void main(String[] args) {
//        HashSet<OptionItem> l=new HashSet<OptionItem>();
//        l.add(new OptionItem("新房",2));
//        l.add(new OptionItem("二手房","4"));
//        l.add(new OptionItem("商铺",16));
//        OptionItem o=new OptionItem("二手房","4");
//        System.out.println(l.contains(new OptionItem("二手房","4")));
//        System.out.println(l.contains(new OptionItem("二手房",null)));
//        System.out.println(l.contains(new OptionItem(null,"4")));
//        System.out.println(l.contains(new OptionItem("二手房","")));
//        System.out.println(l.contains(new OptionItem("","4")));
//
//        System.out.println(o.equals(new OptionItem("二手房","4")));
//        System.out.println(o.equals(new OptionItem("二手房",null)));
//        System.out.println(o.equals(new OptionItem(null,"4")));
//        System.out.println(o.equals(new OptionItem("二手房","")));
//        System.out.println(o.equals(new OptionItem("","4")));
        
    }
}